//贪心 合并果子
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

priority_queue<ll,vector<ll>,greater<ll>> pq;
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	ll n = 0;
	cin>>n;
	//输入到堆中
	while(n--)
	{
		ll tmp=0;
		cin>>tmp;
		pq.push(tmp);
	}
	ll sum = 0;
	while(pq.size()>1)
	{
		//取出最小的和次小的合并
		//总消耗体力中加上这次合并的消耗
		//将合并后的堆放回去，开始新一轮计算
		ll x = pq.top();pq.pop();
		ll y = pq.top();pq.pop();
		ll tmp = x+y;
		sum+=tmp;
		pq.push(tmp);
	}
	cout<<sum;
	
}
